using System;
using System.Collections.Generic;
using UnityEngine;

public class TableTennisGameStartScript : MonoBehaviour
{
	[Serializable]
	public class RoomLookDescriptor
	{
		public RoomLook look;

		public GameObject prefab;
	}

	[Serializable]
	public class GameTypeConfig
	{
		public TableTennisGameTypeBase gameType;

		public MatchType matchType;

		public bool isDefault;

		public bool isTestMatch;
	}

	public List<GameTypeConfig> gameTypes = new List<GameTypeConfig>();

	public bool testMatch;

	public int maxMultiplayerServeTimeouts = 3;

	public GameObject humanPlayerPrefab;

	public GameObject aiPlayerPrefab;

	public GameObject racketPlayerPrefab;

	public GameObject racketAiPrefab;

	public GameObject tutorialPlayerPrefab;

	public GameObject racketRemotePlayer;

	public GameObject remotePlayer;

	public bool dontInstantiatePlayerPrefabs;

	public GameObject ballPrefab;

	public MatchUI ui;

	public ArenaCustomizer arenaCustomizer;

	public GameObject playerAudioPrefab;

	public bool loadArena;

	public TTCameraController cameraController;

	public List<RoomLookDescriptor> rooms = new List<RoomLookDescriptor>();

	public float timeScale = 1f;

	public BallMachineController ballMachine;

	public bool startOnAwake = true;

	protected List<GameObject> instantiatedGameObjects = new List<GameObject>();

	public bool enablePhotonSimulator;

	public GameObject photonSimulator;

	public float maxMultiplayerIntroSeconds = 5f;

	public bool paused { get; protected set; }

	public TableTennisMatchController match { get; protected set; }

	public void DestroyInstantiatedGameObjects()
	{
		foreach (GameObject instantiatedGameObject in instantiatedGameObjects)
		{
			UnityEngine.Object.Destroy(instantiatedGameObject);
		}
		instantiatedGameObjects.Clear();
	}

	public void StartMatch(MatchParameters.MatchParams matchParams)
	{
		MatchParameters.InitParameters = matchParams;
		TableTennisGameTypeBase gameType = null;
		if (matchParams.matchType == MatchType.HitTargets)
		{
			gameType = CreateGameObject<TableTennisHitTargets>();
		}
		StartMatch(matchParams.matchType, gameType);
	}

	public void StartMatch(MatchType matchType, TableTennisGameTypeBase gameType)
	{
		MatchParameters.MatchParams initParameters = MatchParameters.InitParameters;
		match = null;
		switch (matchType)
		{
		case MatchType.MultiPlayer:
			match = CreateGameObject<TableTennisMultiplayerMatchController>();
			break;
		case MatchType.PassAndPlay:
			throw new NotImplementedException();
		default:
			match = CreateGameObject<TableTennisMatchController>();
			break;
		}
		if (photonSimulator != null)
		{
			photonSimulator.SetActive(enablePhotonSimulator && matchType == MatchType.MultiPlayer);
		}
		if (gameType != null && gameType.gameObject != null)
		{
			gameType.gameObject.SetActive(true);
		}
		Time.timeScale = timeScale;
		match.Init(this, ui, gameType);
		if (arenaCustomizer != null)
		{
			arenaCustomizer.matchController = match;
		}
		if (arenaCustomizer != null)
		{
			ObjectLookCustomization arenaForNameOrDefault = ScriptableObjectSingleton<ArenaCustomizations>.instance.GetArenaForNameOrDefault(initParameters.arenaName);
			if (arenaForNameOrDefault != null)
			{
				arenaCustomizer.ApplyCustomization(arenaForNameOrDefault, match);
			}
		}
	}

	private void Awake()
	{
		if (!startOnAwake)
		{
			return;
		}
		MatchParameters.MatchParams initParameters = MatchParameters.InitParameters;
		if (loadArena)
		{
			CreateRoom(initParameters.roomLook);
		}
		foreach (GameTypeConfig gameType in gameTypes)
		{
			if (gameType.gameType != null)
			{
				gameType.gameType.gameObject.SetActive(false);
			}
		}
		TableTennisGameTypeBase gameTypeForMatch = GetGameTypeForMatch(initParameters.matchType);
		StartMatch(initParameters.matchType, gameTypeForMatch);
	}

	private TableTennisGameTypeBase GetGameTypeForMatch(MatchType matchType)
	{
		if (testMatch)
		{
			foreach (GameTypeConfig gameType in gameTypes)
			{
				if (gameType.isTestMatch && testMatch)
				{
					return gameType.gameType;
				}
			}
		}
		TableTennisGameTypeBase result = null;
		foreach (GameTypeConfig gameType2 in gameTypes)
		{
			if (gameType2.matchType == matchType && !gameType2.isTestMatch)
			{
				return gameType2.gameType;
			}
			if (gameType2.isDefault)
			{
				result = gameType2.gameType;
			}
		}
		return result;
	}

	private void CreateRoom(int roomLook)
	{
		RoomLookDescriptor roomLookDescriptor = GetRoomLookDescriptor(roomLook);
		GameObject gameObject = UnityEngine.Object.Instantiate(roomLookDescriptor.prefab);
	}

	private RoomLookDescriptor GetRoomLookDescriptor(int roomLook)
	{
		return rooms[Mathf.Min(roomLook, rooms.Count - 1)];
	}

	public TableTennisPlayerBase CreateHumanPlayer()
	{
		if (MatchParameters.InitParameters.p1Type == PlayerType.avatar)
		{
			return CreateHumanPlayerAvatar();
		}
		if (MatchParameters.InitParameters.p1Type == PlayerType.racket)
		{
			return CreateHumanPlayerRacket();
		}
		return null;
	}

	private TableTennisPlayerBase CreateHumanPlayerAvatar()
	{
		TableTennisHumanPlayer tableTennisHumanPlayer = null;
		if (dontInstantiatePlayerPrefabs)
		{
			tableTennisHumanPlayer = humanPlayerPrefab.GetComponent<TableTennisHumanPlayer>();
		}
		else
		{
			tableTennisHumanPlayer = UnityEngine.Object.Instantiate(humanPlayerPrefab).GetComponent<TableTennisHumanPlayer>();
			instantiatedGameObjects.Add(tableTennisHumanPlayer.gameObject);
		}
		tableTennisHumanPlayer.gameObject.SetActive(true);
		return tableTennisHumanPlayer;
	}

	private TableTennisPlayerBase CreateHumanPlayerRacket()
	{
		TableTennisHumanPlayerRacket tableTennisHumanPlayerRacket = null;
		if (dontInstantiatePlayerPrefabs)
		{
			tableTennisHumanPlayerRacket = racketPlayerPrefab.GetComponent<TableTennisHumanPlayerRacket>();
		}
		else
		{
			tableTennisHumanPlayerRacket = UnityEngine.Object.Instantiate(racketPlayerPrefab).GetComponent<TableTennisHumanPlayerRacket>();
			instantiatedGameObjects.Add(tableTennisHumanPlayerRacket.gameObject);
		}
		tableTennisHumanPlayerRacket.gameObject.SetActive(true);
		return tableTennisHumanPlayerRacket;
	}

	private TableTennisPlayerBase CreateAiPlayerRacket()
	{
		TableTennisAiPlayerRacket tableTennisAiPlayerRacket = null;
		if (dontInstantiatePlayerPrefabs)
		{
			tableTennisAiPlayerRacket = racketAiPrefab.GetComponent<TableTennisAiPlayerRacket>();
		}
		else
		{
			tableTennisAiPlayerRacket = UnityEngine.Object.Instantiate(racketAiPrefab).GetComponent<TableTennisAiPlayerRacket>();
			instantiatedGameObjects.Add(tableTennisAiPlayerRacket.gameObject);
		}
		tableTennisAiPlayerRacket.gameObject.SetActive(true);
		return tableTennisAiPlayerRacket;
	}

	private TableTennisPlayerBase CreateAiPlayerAvatar()
	{
		TableTennisAIPlayer tableTennisAIPlayer = null;
		if (dontInstantiatePlayerPrefabs)
		{
			tableTennisAIPlayer = aiPlayerPrefab.GetComponent<TableTennisAIPlayer>();
		}
		else
		{
			tableTennisAIPlayer = UnityEngine.Object.Instantiate(aiPlayerPrefab).GetComponent<TableTennisAIPlayer>();
			instantiatedGameObjects.Add(tableTennisAIPlayer.gameObject);
		}
		tableTennisAIPlayer.gameObject.SetActive(true);
		return tableTennisAIPlayer;
	}

	public TableTennisPlayerBase CreateAiPlayer()
	{
		if (MatchParameters.InitParameters.p2Type == PlayerType.avatar)
		{
			return CreateAiPlayerAvatar();
		}
		if (MatchParameters.InitParameters.p2Type == PlayerType.racket)
		{
			return CreateAiPlayerRacket();
		}
		return null;
	}

	public TableTennisPlayerBase CreateTutorialPlayer()
	{
		TutorialPlayer tutorialPlayer = null;
		if (dontInstantiatePlayerPrefabs)
		{
			tutorialPlayer = tutorialPlayerPrefab.GetComponent<TutorialPlayer>();
		}
		else
		{
			tutorialPlayer = UnityEngine.Object.Instantiate(tutorialPlayerPrefab).GetComponent<TutorialPlayer>();
			instantiatedGameObjects.Add(tutorialPlayer.gameObject);
		}
		tutorialPlayer.gameObject.SetActive(true);
		return tutorialPlayer;
	}

	public TableTennisPlayerBase CreateRemotePlayer()
	{
		if (MatchParameters.InitParameters.p2Type == PlayerType.avatar)
		{
			return CreateRemotePlayerAvatar();
		}
		if (MatchParameters.InitParameters.p2Type == PlayerType.racket)
		{
			return CreateRemotePlayerRacket();
		}
		return null;
	}

	private TableTennisPlayerBase CreateRemotePlayerAvatar()
	{
		TableTennisRemotePlayer tableTennisRemotePlayer = null;
		if (dontInstantiatePlayerPrefabs)
		{
			tableTennisRemotePlayer = remotePlayer.GetComponent<TableTennisRemotePlayer>();
		}
		else
		{
			tableTennisRemotePlayer = UnityEngine.Object.Instantiate(remotePlayer).GetComponent<TableTennisRemotePlayer>();
			instantiatedGameObjects.Add(tableTennisRemotePlayer.gameObject);
		}
		tableTennisRemotePlayer.gameObject.SetActive(true);
		return tableTennisRemotePlayer;
	}

	private TableTennisPlayerBase CreateRemotePlayerRacket()
	{
		TableTennisRemotePlayerRacket tableTennisRemotePlayerRacket = null;
		if (dontInstantiatePlayerPrefabs)
		{
			tableTennisRemotePlayerRacket = racketRemotePlayer.GetComponent<TableTennisRemotePlayerRacket>();
		}
		else
		{
			tableTennisRemotePlayerRacket = UnityEngine.Object.Instantiate(racketRemotePlayer).GetComponent<TableTennisRemotePlayerRacket>();
			instantiatedGameObjects.Add(tableTennisRemotePlayerRacket.gameObject);
		}
		tableTennisRemotePlayerRacket.gameObject.SetActive(true);
		return tableTennisRemotePlayerRacket;
	}

	public BallMachineController CreateBallMachine()
	{
		BallMachineController ballMachineController = null;
		if (dontInstantiatePlayerPrefabs)
		{
			ballMachineController = ballMachine;
		}
		else
		{
			ballMachineController = UnityEngine.Object.Instantiate(ballMachine.gameObject).GetComponent<BallMachineController>();
			instantiatedGameObjects.Add(ballMachineController.gameObject);
		}
		return ballMachineController;
	}

	public PhysicsBall CreateBall()
	{
		PhysicsBall component = UnityEngine.Object.Instantiate(ballPrefab).GetComponent<PhysicsBall>();
		instantiatedGameObjects.Add(component.gameObject);
		ui.ballFollower.pBall = component;
		return component;
	}

	public T CreateGameObject<T>() where T : MonoBehaviour
	{
		GameObject gameObject = new GameObject(typeof(T).ToString());
		gameObject.transform.parent = base.transform.parent;
		instantiatedGameObjects.Add(gameObject);
		return gameObject.AddComponent<T>();
	}

	public void Update()
	{
		if (Input.GetKeyDown(KeyCode.P))
		{
			OnPauseTime();
		}
	}

	public void OnPauseTime()
	{
		if (Time.timeScale == 0f)
		{
			Time.timeScale = timeScale;
		}
		else
		{
			Time.timeScale = 0f;
		}
		paused = !paused;
		TTCameraController component = Camera.main.GetComponent<TTCameraController>();
		component.Refresh();
	}
}
